package com.net.util.request;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;
/**
 * 消息转换器
 * 
 *
 */
public class WiselyMessageConverter extends AbstractHttpMessageConverter<Object> {

	//自定义媒体类型
    public WiselyMessageConverter(){
        super(new MediaType("application", "json", Charset.forName("UTF-8")));
    }
    
	@Override
	protected boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		 return Object.class.isAssignableFrom(clazz);
	}

	@Override
	protected Object readInternal(Class clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException {
		 String temp = StreamUtils.copyToString(inputMessage.getBody(), 
				 Charset.forName("UTF-8"));
				         String[] tempArr = temp.split("-");
				         return new Object();
	}

	@Override
	protected void writeInternal(Object t, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		// TODO Auto-generated method stub
		String out = "hello:" + "-" ;
        outputMessage.getBody().write(out.getBytes());
	}
	
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WiselyMessageConverter converter = new WiselyMessageConverter();
        converters.add(converter);
    }
}
